import json

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.
from django.core.files.storage import default_storage
class Images(APIView):
    def post(self,reqeust):
        file=reqeust.data.get('file')
        types=['jpg','png']
        if file.name.split('.')[-1] not in types:
            return Response({"code":400,'msg':'文件不符合格式要求'})

        if file.size>1024*1024*3:
            return Response({"code":402,'msg':'文件太大了'})

        uploadsfile=default_storage.save(f'img/{file.name}',file)
        url=default_storage.url(uploadsfile)
        return Response({"url":url})


from utils.send import send_message
import re,random
from utils.myredis import redis_tool
class Sendcode(APIView):
    def get(self,request):
        phone=request.query_params.get('phone')

        number=re.match(r"^1[3-9]\d{9}$",phone)
        if not number:
            return Response({"msg":"手机号不对"})
        code=random.randint(1000,9999)
        ser=send_message(phone,code)

        redis_tool.setcode(phone,code)

        jsonser=json.loads(ser)
        if jsonser['statusCode']=='000000':
            return Response({"msg": '发送成功', "code": 200})
        else:
            return Response({"msg": '发送失败', "code": 400})






